
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>service · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="nirvana.html" />
    
    
    <link rel="prev" href="router.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    快速入门
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../guides/project.html">
            
                <a href="../guides/project.html">
            
                    
                    项目结构和初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../guides/api.html">
            
                <a href="../guides/api.html">
            
                    
                    API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../guides/doc.html">
            
                <a href="../guides/doc.html">
            
                    
                    生成 API 文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../guides/client.html">
            
                <a href="../guides/client.html">
            
                    
                    生成客户端
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../concepts/cp.html">
            
                <a href="../concepts/cp.html">
            
                    
                    Consumer && Producer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../concepts/operator.html">
            
                <a href="../concepts/operator.html">
            
                    
                    Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../concepts/filter.html">
            
                <a href="../concepts/filter.html">
            
                    
                    Filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../concepts/prefab.html">
            
                <a href="../concepts/prefab.html">
            
                    
                    Prefab
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../concepts/modifier.html">
            
                <a href="../concepts/modifier.html">
            
                    
                    Modifier
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../concepts/middleware.html">
            
                <a href="../concepts/middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../concepts/context.html">
            
                <a href="../concepts/context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../concepts/source.html">
            
                <a href="../concepts/source.html">
            
                    
                    Parameter Generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../concepts/destination.html">
            
                <a href="../concepts/destination.html">
            
                    
                    Destination Handler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../concepts/error.html">
            
                <a href="../concepts/error.html">
            
                    
                    错误包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../concepts/method.html">
            
                <a href="../concepts/method.html">
            
                    
                    方法包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../concepts/configurer.html">
            
                <a href="../concepts/configurer.html">
            
                    
                    配置器机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../concepts/plugin.html">
            
                <a href="../concepts/plugin.html">
            
                    
                    插件机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../concepts/clients.html">
            
                <a href="../concepts/clients.html">
            
                    
                    多客户端整合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    插件
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../plugins/logger.html">
            
                <a href="../plugins/logger.html">
            
                    
                    系统日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../plugins/reqlog.html">
            
                <a href="../plugins/reqlog.html">
            
                    
                    请求日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../plugins/metrics.html">
            
                <a href="../plugins/metrics.html">
            
                    
                    监控指标插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../plugins/tracing.html">
            
                <a href="../plugins/tracing.html">
            
                    
                    请求追踪插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../plugins/profiling.html">
            
                <a href="../plugins/profiling.html">
            
                    
                    性能分析插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../plugins/version.html">
            
                <a href="../plugins/version.html">
            
                    
                    版本信息插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../plugins/healthcheck.html">
            
                <a href="../plugins/healthcheck.html">
            
                    
                    健康检查插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    框架开发者指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="start.html">
            
                <a href="start.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="log.html">
            
                <a href="log.html">
            
                    
                    log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="errors.html">
            
                <a href="errors.html">
            
                    
                    errors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="definition.html">
            
                <a href="definition.html">
            
                    
                    definition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="method.html">
            
                <a href="method.html">
            
                    
                    definition/method
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="converter.html">
            
                <a href="converter.html">
            
                    
                    operators/converter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="validator.html">
            
                <a href="validator.html">
            
                    
                    operators/validator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="router.html">
            
                <a href="router.html">
            
                    
                    service/router
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.9" data-path="service.html">
            
                <a href="service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="nirvana.html">
            
                <a href="nirvana.html">
            
                    
                    nirvana
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="config.html">
            
                <a href="config.html">
            
                    
                    config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="rest.html">
            
                <a href="rest.html">
            
                    
                    rest
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="utils.html">
            
                <a href="utils.html">
            
                    
                    utils
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="command.html">
            
                <a href="command.html">
            
                    
                    cmd/nirvana
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >service</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="service-&#x5305;">service &#x5305;</h1>
<p>service &#x5305;&#x5B9E;&#x73B0;&#x4E86; Nirvana &#x7684; API &#x5904;&#x7406;&#x6846;&#x67B6;&#xFF1A;</p>
<pre><code>           Service.ServeHTTP()
         ----------------------
         &#x2193;                    &#x2191;
|-----Filters------|          &#x2191;
         &#x2193;                    &#x2191;
|---Router Match---|          &#x2191;
         &#x2193;                    &#x2191;
|-------------Middlewares------------|
         &#x2193;                    &#x2191;
|-------------Executor---------------|
         &#x2193;                    &#x2191;
|-ParameterGenerators-|-DestinationHandlers-|
         &#x2193;                    &#x2191;
|------------User Function-----------|
</code></pre><p>service &#x5305;&#x7684;&#x5165;&#x53E3;&#x662F; Builder&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Builder builds service.</span>
<span class="hljs-keyword">type</span> Builder <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// Logger returns logger of builder.</span>
    Logger() log.Logger
    <span class="hljs-comment">// SetLogger sets logger to server.</span>
    SetLogger(logger log.Logger)
    <span class="hljs-comment">// Modifier returns modifier of builder.</span>
    Modifier() DefinitionModifier
    <span class="hljs-comment">// SetModifier sets definition modifier.</span>
    SetModifier(m DefinitionModifier)
    <span class="hljs-comment">// Filters returns all request filters.</span>
    Filters() []Filter
    <span class="hljs-comment">// AddFilters add filters to filter requests.</span>
    AddFilter(filters ...Filter)
    <span class="hljs-comment">// AddDescriptors adds descriptors to router.</span>
    AddDescriptor(descriptors ...definition.Descriptor) error
    <span class="hljs-comment">// Middlewares returns all router middlewares.</span>
    Middlewares() <span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>][]definition.Middleware
    <span class="hljs-comment">// Definitions returns all definitions. If a modifier exists, it will be executed.</span>
    Definitions() <span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>][]definition.Definition
    <span class="hljs-comment">// Build builds a service to handle request.</span>
    Build() (Service, error)
}

<span class="hljs-keyword">type</span> Service <span class="hljs-keyword">interface</span> {
    http.Handler
}

<span class="hljs-comment">// DefinitionModifier is used in Server. It&apos;s used to modify definition.</span>
<span class="hljs-comment">// If you want to add some common data into all definitions, you can write</span>
<span class="hljs-comment">// a customized modifier for it.</span>
<span class="hljs-keyword">type</span> DefinitionModifier <span class="hljs-keyword">func</span>(d *definition.Definition)


<span class="hljs-comment">// Filter can filter request. It has the highest priority in a request</span>
<span class="hljs-comment">// lifecycle. It runs before router matching.</span>
<span class="hljs-comment">// If a filter return false, that means the request should be filtered.</span>
<span class="hljs-comment">// If a filter want to filter a request, it should handle the request</span>
<span class="hljs-comment">// by itself.</span>
<span class="hljs-keyword">type</span> Filter <span class="hljs-keyword">func</span>(resp http.ResponseWriter, req *http.Request) <span class="hljs-keyword">bool</span>
</code></pre>
<p>Builder &#x6784;&#x5EFA; Service &#x6765;&#x63D0;&#x4F9B; HTTP &#x670D;&#x52A1;&#x3002;&#x56E0;&#x6B64; Builder &#x63D0;&#x4F9B;&#x4E86;&#x591A;&#x4E2A;&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x8BBE;&#x7F6E;&#x751F;&#x6210;&#x670D;&#x52A1;&#x9700;&#x8981;&#x7684;&#x65E5;&#x5FD7;&#xFF0C;Definition &#x4FEE;&#x6539;&#x5668;&#xFF0C;&#x8BF7;&#x6C42;&#x8FC7;&#x6EE4;&#x5668;&#xFF0C;API &#x63CF;&#x8FF0;&#x7B26;&#x3002;&#x6784;&#x5EFA;&#x5B8C;&#x6210;&#x7684; Service &#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x4E00;&#x4E2A; http.Handler&#xFF0C;&#x7528;&#x6765;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x3002;</p>
<p>&#x5176;&#x4E2D; Definition &#x4FEE;&#x6539;&#x5668;&#x7528;&#x4E8E;&#x5728;&#x751F;&#x6210;&#x8DEF;&#x7531;&#x4E4B;&#x524D;&#x4FEE;&#x6539; API Definition&#x3002;&#x8BF7;&#x6C42;&#x8FC7;&#x6EE4;&#x5668;&#x5219;&#x662F;&#x5728; Service &#x6267;&#x884C;&#x7684;&#x65F6;&#x5019;&#x624D;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x8BF7;&#x6C42;&#x8FC7;&#x6EE4;&#x5668;&#x7684;&#x4F18;&#x5148;&#x7EA7;&#x9AD8;&#x4E8E;&#x8DEF;&#x7531;&#x5339;&#x914D;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5728;&#x8DEF;&#x7531;&#x5339;&#x914D;&#x4E4B;&#x524D;&#xFF0C;&#x8BF7;&#x6C42;&#x5C31;&#x6709;&#x53EF;&#x80FD;&#x88AB;&#x8FC7;&#x6EE4;&#x5668;&#x76F4;&#x63A5;&#x8FC7;&#x6EE4;&#x6389;&#x3002;</p>
<p>Builder &#x8FD8;&#x4F1A;&#x5C06; API Definition &#x8F6C;&#x6362;&#x4E3A;&#x8DEF;&#x7531;&#x9700;&#x8981;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x6D89;&#x53CA;&#x5230;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#xFF1A;</p>
<ol>
<li><p>&#x5BF9;&#x5E94; Consumes &#x548C; Produces &#x7684; Consumer &#x548C; Producer<br>Consumer &#x9488;&#x5BF9;&#x8BF7;&#x6C42;&#x7684; body&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x8F6C;&#x6362;&#x4E3A;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x9700;&#x8981;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF08;&#x901A;&#x5E38;&#x662F;&#x7ED3;&#x6784;&#x4F53;&#xFF09;&#x3002;<br>Producer &#x5219;&#x662F;&#x5C06;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x8F6C;&#x6362;&#x5E76;&#x5199;&#x5165;&#x5230;&#x54CD;&#x5E94;&#x7684; body &#x4E2D;&#x3002;  </p>
<pre><code class="lang-go"><span class="hljs-comment">// Consumer handles specifically typed data from a reader and unmarshals it into an object.</span>
<span class="hljs-keyword">type</span> Consumer <span class="hljs-keyword">interface</span> {
   <span class="hljs-comment">// ContentType returns a HTTP MIME type.</span>
   ContentType() <span class="hljs-keyword">string</span>
   <span class="hljs-comment">// Consume unmarshals data from r into v.</span>
   Consume(r io.Reader, v <span class="hljs-keyword">interface</span>{}) error
}

<span class="hljs-comment">// Producer marshals an object to specifically typed data and write it into a writer.</span>
<span class="hljs-keyword">type</span> Producer <span class="hljs-keyword">interface</span> {
   <span class="hljs-comment">// ContentType returns a HTTP MIME type.</span>
   ContentType() <span class="hljs-keyword">string</span>
   <span class="hljs-comment">// Produce marshals v to data and write to w.</span>
   Produce(w io.Writer, v <span class="hljs-keyword">interface</span>{}) error
}
</code></pre>
</li>
<li>&#x5BF9;&#x5E94; Prefab &#x7C7B;&#x578B;&#x7684; Prefab &#x751F;&#x6210;&#x5668;<br>&#x8FD9;&#x4E2A;&#x751F;&#x6210;&#x5668;&#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x9700;&#x8981;&#x7684;&#x7279;&#x5B9A;&#x5B9E;&#x4F8B;&#xFF0C;&#x4E00;&#x822C;&#x662F;&#x670D;&#x52A1;&#x7AEF;&#x5B9E;&#x4F8B;&#xFF0C;&#x5373;&#x4E0D;&#x662F;&#x4ECE;&#x8BF7;&#x6C42;&#x91CC;&#x83B7;&#x53D6;&#x7684;&#x6570;&#x636E;&#x800C;&#x751F;&#x6210;&#x7684;&#x3002;<br>service &#x5305;&#x91CC;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A; Context Prefab &#x751F;&#x6210;&#x5668;&#xFF0C;&#x7B80;&#x5355;&#x7684;&#x5C06;&#x53C2;&#x6570;&#x91CC;&#x7684; context &#x8FD4;&#x56DE;&#x51FA;&#x53BB;&#xFF0C;&#x4F9B;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x4F7F;&#x7528;&#x3002;<pre><code class="lang-go"><span class="hljs-comment">// Prefab creates instances for internal type. These instances are not</span>
<span class="hljs-comment">// unmarshaled form http request data.</span>
<span class="hljs-keyword">type</span> Prefab <span class="hljs-keyword">interface</span> {
   <span class="hljs-comment">// Name returns prefab name.</span>
   Name() <span class="hljs-keyword">string</span>
   <span class="hljs-comment">// Type is instance type.</span>
   Type() reflect.Type
   <span class="hljs-comment">// Make makes an instance.</span>
   Make(ctx context.Context) (<span class="hljs-keyword">interface</span>{}, error)
}
</code></pre>
</li>
<li>&#x5BF9;&#x5E94; golang &#x57FA;&#x7840;&#x7C7B;&#x578B;&#x7684;&#x8F6C;&#x6362;&#x5668;<br>&#x8FD9;&#x4E9B;&#x8F6C;&#x6362;&#x5668;&#x4E00;&#x822C;&#x662F;&#x7528;&#x4E8E;&#x5C06;&#x8BF7;&#x6C42;&#x91CC;&#x7684; query&#xFF0C;header &#x7B49;&#x7B80;&#x5355;&#x5B57;&#x7B26;&#x4E32;&#x6570;&#x636E;&#x8F6C;&#x6362;&#x4E3A; golang &#x7684;&#x57FA;&#x7840;&#x7C7B;&#x578B;&#xFF0C;&#x4F9B;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x4F7F;&#x7528;&#x3002;<pre><code class="lang-go"><span class="hljs-comment">// Converter is used to convert []string to specific type. Data must have one</span>
<span class="hljs-comment">// element at least or it will panic.</span>
<span class="hljs-keyword">type</span> Converter <span class="hljs-keyword">func</span>(ctx context.Context, data []<span class="hljs-keyword">string</span>) (<span class="hljs-keyword">interface</span>{}, error)
</code></pre>
</li>
<li>&#x7528;&#x4E8E;&#x5C01;&#x88C5;&#x8BF7;&#x6C42;&#x7684; ValueContainer<br>&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x662F;&#x5BF9; Request &#x7684;&#x4E00;&#x6B21;&#x5C01;&#x88C5;&#xFF0C;&#x65B9;&#x4FBF;&#x83B7;&#x53D6;&#x5BF9;&#x5E94;&#x4F4D;&#x7F6E;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x6570;&#x636E;&#x3002;<pre><code class="lang-go"><span class="hljs-comment">// ValueContainer contains values from a request.</span>
<span class="hljs-keyword">type</span> ValueContainer <span class="hljs-keyword">interface</span> {
   <span class="hljs-comment">// Path returns path value by key.</span>
   Path(key <span class="hljs-keyword">string</span>) (<span class="hljs-keyword">string</span>, <span class="hljs-keyword">bool</span>)
   <span class="hljs-comment">// Query returns value from query string.</span>
   Query(key <span class="hljs-keyword">string</span>) ([]<span class="hljs-keyword">string</span>, <span class="hljs-keyword">bool</span>)
   <span class="hljs-comment">// Header returns value by header key.</span>
   Header(key <span class="hljs-keyword">string</span>) ([]<span class="hljs-keyword">string</span>, <span class="hljs-keyword">bool</span>)
   <span class="hljs-comment">// Form returns value from request. It is valid when</span>
   <span class="hljs-comment">// http &quot;Content-Type&quot; is &quot;application/x-www-form-urlencoded&quot;</span>
   <span class="hljs-comment">// or &quot;multipart/form-data&quot;.</span>
   Form(key <span class="hljs-keyword">string</span>) ([]<span class="hljs-keyword">string</span>, <span class="hljs-keyword">bool</span>)
   <span class="hljs-comment">// File returns a file reader when &quot;Content-Type&quot; is &quot;multipart/form-data&quot;.</span>
   File(key <span class="hljs-keyword">string</span>) (multipart.File, <span class="hljs-keyword">bool</span>)
   <span class="hljs-comment">// Body returns a reader to read data from request body.</span>
   <span class="hljs-comment">// The reader only can read once.</span>
   Body() (reader io.ReadCloser, contentType <span class="hljs-keyword">string</span>, ok <span class="hljs-keyword">bool</span>)
}
</code></pre>
</li>
<li>&#x7528;&#x4E8E;&#x5C01;&#x88C5;&#x54CD;&#x5E94;&#x7684; ResponseWriter<br>ResponseWriter &#x662F;&#x5BF9; http.ResponseWriter &#x7684;&#x4E00;&#x4E2A;&#x6269;&#x5C55;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E9B;&#x529F;&#x80FD;&#x65B9;&#x4FBF;&#x4E2D;&#x95F4;&#x4EF6;&#x4F7F;&#x7528;&#x3002;<pre><code class="lang-go"><span class="hljs-comment">// ResponseWriter extends http.ResponseWriter.</span>
<span class="hljs-keyword">type</span> ResponseWriter <span class="hljs-keyword">interface</span> {
   http.ResponseWriter
   <span class="hljs-comment">// HeaderWritable can check whether WriteHeader() has</span>
   <span class="hljs-comment">// been called. If the method returns false, you should</span>
   <span class="hljs-comment">// not recall WriteHeader().</span>
   HeaderWritable() <span class="hljs-keyword">bool</span>
   <span class="hljs-comment">// StatusCode returns status code.</span>
   StatusCode() <span class="hljs-keyword">int</span>
   <span class="hljs-comment">// ContentLength returns the length of written content.</span>
   ContentLength() <span class="hljs-keyword">int</span>
}
</code></pre>
</li>
<li>&#x7528;&#x4E8E;&#x5408;&#x5E76;&#x8BF7;&#x6C42;&#x548C;&#x54CD;&#x5E94;&#x7684; Context<br>HTTPContext &#x5B9E;&#x73B0;&#x4E86; Context &#x63A5;&#x53E3;&#xFF0C;&#x5305;&#x88C5;&#x4E86;&#x8BF7;&#x6C42;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x4F5C;&#x4E3A;&#x8DEF;&#x7531;&#x4E0A;&#x4E0B;&#x6587;&#x4F7F;&#x7528;&#x3002;<pre><code class="lang-go"><span class="hljs-comment">// HTTPContext describes an http context.</span>
<span class="hljs-keyword">type</span> HTTPContext <span class="hljs-keyword">interface</span> {
   Request() *http.Request
   ResponseWriter() ResponseWriter
   ValueContainer() ValueContainer
   RoutePath() <span class="hljs-keyword">string</span>
}
</code></pre>
</li>
<li>&#x7528;&#x4E8E;&#x751F;&#x6210;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x7684; ParameterGenerator<br>ParameterGenerator &#x662F;&#x771F;&#x6B63;&#x7684;&#x53C2;&#x6570;&#x751F;&#x6210;&#x5668;&#xFF0C;&#x901A;&#x8FC7;&#x8C03;&#x7528; Consumer&#xFF0C;Converter&#xFF0C;Prefab &#x7B49;&#x6765;&#x5B8C;&#x6210;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x751F;&#x6210;&#x3002;<pre><code class="lang-go"><span class="hljs-comment">// ParameterGenerator is used to generate object for a parameter.</span>
<span class="hljs-keyword">type</span> ParameterGenerator <span class="hljs-keyword">interface</span> {
   <span class="hljs-comment">// Source returns the source generated by current generator.</span>
   Source() definition.Source
   <span class="hljs-comment">// Validate validates whether defaultValue and target type is valid.</span>
   Validate(name <span class="hljs-keyword">string</span>, defaultValue <span class="hljs-keyword">interface</span>{}, target reflect.Type) error
   <span class="hljs-comment">// Generate generates an object by data from value container.</span>
   Generate(ctx context.Context, vc ValueContainer, consumers []Consumer, name <span class="hljs-keyword">string</span>, target reflect.Type) (<span class="hljs-keyword">interface</span>{}, error)
}
</code></pre>
</li>
<li><p>&#x7528;&#x4E8E;&#x5C06;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x5199;&#x5165; Response &#x7684; DestinationHandler<br>DestinationHandler &#x662F;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x5904;&#x7406;&#x5668;&#xFF0C;&#x901A;&#x8FC7;&#x8C03;&#x7528; Producer &#x5C06;&#x8FD4;&#x56DE;&#x503C;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x8282;&#x5199;&#x5165;&#x54CD;&#x5E94;&#x4E2D;&#x3002;<br>&#x5728; DestinationHandler &#x4E2D;&#xFF0C;&#x9519;&#x8BEF;&#x662F;&#x4F1A;&#x8FDB;&#x884C;&#x7279;&#x6B8A;&#x5904;&#x7406;&#x7684;&#x3002;&#x5982;&#x679C;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x7684;&#x9519;&#x8BEF;&#x7B26;&#x5408; Error &#x63A5;&#x53E3;&#xFF0C;&#x5219;&#x4F1A;&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x6765;&#x751F;&#x6210;&#x9519;&#x8BEF;&#x7801;&#x548C;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Error is a common interface for error.</span>
<span class="hljs-comment">// If an error implements the interface, type handlers can</span>
<span class="hljs-comment">// use Code() to get a specified HTTP status code.</span>
<span class="hljs-keyword">type</span> Error <span class="hljs-keyword">interface</span> {
   <span class="hljs-comment">// Code is a HTTP status code.</span>
   Code() <span class="hljs-keyword">int</span>
   <span class="hljs-comment">// Message is an object which contains information of the error.</span>
   Message() <span class="hljs-keyword">interface</span>{}
}

<span class="hljs-keyword">const</span> (
   <span class="hljs-comment">// HighPriority for error type.</span>
   <span class="hljs-comment">// If an error occurs, ignore meta and data.</span>
   HighPriority <span class="hljs-keyword">int</span> = <span class="hljs-number">100</span>
   <span class="hljs-comment">// MediumPriority for meta type.</span>
   MediumPriority <span class="hljs-keyword">int</span> = <span class="hljs-number">200</span>
   <span class="hljs-comment">// LowPriority for data type.</span>
   LowPriority <span class="hljs-keyword">int</span> = <span class="hljs-number">300</span>
)

<span class="hljs-comment">// DestinationHandler is used to handle the results from API handlers.</span>
<span class="hljs-keyword">type</span> DestinationHandler <span class="hljs-keyword">interface</span> {
   <span class="hljs-comment">// Type returns definition.Type which the type handler can handle.</span>
   Destination() definition.Destination
   <span class="hljs-comment">// Priority returns priority of the type handler. Type handler with higher priority will prior execute.</span>
   Priority() <span class="hljs-keyword">int</span>
   <span class="hljs-comment">// Validate validates whether the type handler can handle the target type.</span>
   Validate(target reflect.Type) error
   <span class="hljs-comment">// Handle handles a value. If the handler has something wrong, it should return an error.</span>
   <span class="hljs-comment">// The handler descides how to deal with value by producers and status code.</span>
   <span class="hljs-comment">// The status code is a success status code. If everything is ok, the handler should use the status code.</span>
   <span class="hljs-comment">//</span>
   <span class="hljs-comment">// There are three cases for return values (goon means go on or continue):</span>
   <span class="hljs-comment">// 1. go on is true, err is nil.</span>
   <span class="hljs-comment">//    It means that current type handler did nothing (or looks like did nothing) and next type handler</span>
   <span class="hljs-comment">//    should take the context.</span>
   <span class="hljs-comment">// 2. go on is false, err is nil.</span>
   <span class="hljs-comment">//    It means that current type handler has finished the context and next type handler should not run.</span>
   <span class="hljs-comment">// 3. err is not nil</span>
   <span class="hljs-comment">//    It means that current type handler handled the context but something wrong. All subsequent type</span>
   <span class="hljs-comment">//    handlers should not run.</span>
   Handle(ctx context.Context, producers []Producer, code <span class="hljs-keyword">int</span>, value <span class="hljs-keyword">interface</span>{}) (goon <span class="hljs-keyword">bool</span>, err error)
}
</code></pre>
</li>
</ol>
<p><strong>&#x6CE8;&#xFF1A;&#x4EE5;&#x4E0A;&#x6BCF;&#x4E2A;&#x63A5;&#x53E3;&#x5BF9;&#x5E94;&#x7684;&#x5B9E;&#x4F8B;&#x90FD;&#x662F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x76F8;&#x5173;&#x7684;&#x51FD;&#x6570;&#x6CE8;&#x518C;&#x548C;&#x4FEE;&#x6539;&#x7684;&#x3002;</strong></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="router.html" class="navigation navigation-prev " aria-label="Previous page: service/router">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="nirvana.html" class="navigation navigation-next " aria-label="Next page: nirvana">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"service","level":"1.5.9","depth":2,"next":{"title":"nirvana","level":"1.5.10","depth":2,"path":"topics/nirvana.md","ref":"topics/nirvana.md","articles":[]},"previous":{"title":"service/router","level":"1.5.8","depth":2,"path":"topics/router.md","ref":"topics/router.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*"},"file":{"path":"topics/service.md","mtime":"2019-08-06T09:48:58.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-20T10:33:16.586Z"},"basePath":"..","book":{"language":"zh-hans"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

